'use strict';

var log4js = require('log4js'),
  mkdirp = require('mkdirp'),
  {resolve} = require('path'),
  LOGS_DIR;

//if (process.env.NODE_ENV == 'profile') {
//LOGS_DIR = resolve(__dirname, '../../logs');
//} else {
//LOGS_DIR = '/opt/logs/nodejs';
//}
LOGS_DIR = resolve(__dirname, '../../logs');

mkdirp.sync(LOGS_DIR, parseInt('0777', 8));

log4js.configure({
  appenders: [
    {
      type: 'console',
      layout: {
        type: 'pattern',
        pattern: '[%z][%d{ISO8601_WITH_TZ_OFFSET}][%-5p] %m',
      },
    },
    {
      type: `file`,
      filename: `${LOGS_DIR}/access.log`,
      usefsync: true,
      category: 'access',
      layout: {
        type: 'pattern',
        pattern: '[%z][%d{ISO8601_WITH_TZ_OFFSET}][%-5p] %m',
      },
    },
    {
      type: `file`,
      filename: `${LOGS_DIR}/error.log`,
      usefsync: true,
      category: 'error',
      layout: {
        type: 'pattern',
        pattern: '[%z][%d{ISO8601_WITH_TZ_OFFSET}][%-5p] %m',
      },
    }, //2016-04-15T19:01:57+0800 ERROR xxxmsg
  ],
  levels: {
    "access": "DEBUG",
    //access: 'INFO',
    error: 'ERROR',
  },
});

var accessLogger = log4js.getLogger('access');
var errorLogger = log4js.getLogger('error');

var TagLogger = (exports.TagLogger = function(tag) {
  var self = this;
  self.trace = content => {
    accessLogger.trace('[' + tag + ']' + content);
  };
  self.debug = content => {
    accessLogger.debug('[' + tag + ']' + content);
  };
  self.info = content => {
    accessLogger.info('[' + tag + ']' + content);
  };
  self.warn = content => {
    accessLogger.warn('[' + tag + ']' + content);
  };

  self.error = content => {
    var msg = '[' + tag + ']' + content;
    errorLogger.error(msg);
  };
  self.fatal = content => {
    var msg = '[' + tag + ']' + content;
    errorLogger.fatal(msg);
  };
});
